import Vue from 'vue';
import VueRouter from 'vue-router';
import { get } from '@/common/utils';

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/Login.vue'),
  },
  {
    path: '/regist',
    name: 'Regist',
    component: () => import(/* webpackChunkName: "regist" */ '@/views/regist/Regist.vue'),
  },
  {
    path: '/admin',
    component: () => import(/* webpackChunkName: "admin" */ '@/views/admin/Admin.vue'),
    children: [
      {
        path: '/',
        name: 'Admin',
        redirect: 'userinfo',
      },
      {
        path: 'userinfo',
        component: () => import(/* webpackChunkName: "userinfo" */ '@/views/userinfo/Userinfo.vue'),
      },
      {
        path: 'mall',
        component: () => import(/* webpackChunkName: "mall" */ '@/views/mall/Mall.vue'),
      },
    ],
    beforeEnter(to, from, next) {
      if (get('user')) {
        next();
      } else {
        next('/login');
      }
    },
  },

];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;
